<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :inline="true" :model="queryParams" label-width="120px">
            <el-form-item label="描述" prop="description">
              <el-input v-model="queryParams.description" clearable placeholder="请输入描述" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="是否删除" prop="isDeleted">
              <!-- <el-input v-model="queryParams.isDeleted" clearable placeholder="请输入是否删除：0-未删除，1-已删除" @keyup.enter="handleQuery" /> -->
              <el-select v-model="queryParams.isDeleted" class="m-2" placeholder="请选择">
                <el-option v-for="item in isDeletedOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="订单号" prop="orderNumber">
              <el-input v-model="queryParams.orderNumber" clearable placeholder="请输入订单号" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="车辆号" prop="vehicleNumber">
              <el-input v-model="queryParams.vehicleNumber" clearable placeholder="请输入车辆号" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="工艺路线编码" prop="processRouteCode">
              <el-input v-model="queryParams.processRouteCode" clearable placeholder="请输入工艺路线编码"
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="工序号" prop="processNumber">
              <el-input v-model="queryParams.processNumber" clearable placeholder="请输入工序号" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="工序名称" prop="processName">
              <el-input v-model="queryParams.processName" clearable placeholder="请输入工序名称" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="工作中心编码" prop="workCenterCode">
              <el-input v-model="queryParams.workCenterCode" clearable placeholder="请输入工作中心编码"
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="工作中心名称" prop="workCenterName">
              <el-input v-model="queryParams.workCenterName" clearable placeholder="请输入工作中心名称"
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="工作中心描述" prop="workCenterDescription">
              <el-input v-model="queryParams.workCenterDescription" clearable placeholder="请输入工作中心描述"
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="计划开始时间" prop="planStartTime">
              <el-date-picker v-model="queryParams.planStartTime" clearable placeholder="请选择计划开始时间" type="date"
                value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="计划完工时间" prop="planEndTime">
              <el-date-picker v-model="queryParams.planEndTime" clearable placeholder="请选择计划完工时间" type="date"
                value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="实际开工时间" prop="actualStartTime">
              <el-date-picker v-model="queryParams.actualStartTime" clearable placeholder="请选择实际开工时间" type="date"
                value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="实际完工时间" prop="actualEndTime">
              <el-date-picker v-model="queryParams.actualEndTime" clearable placeholder="请选择实际完工时间" type="date"
                value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="作业人ID" prop="operator">
              <el-input v-model="queryParams.operator" clearable placeholder="请输入作业人ID" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['painting:workPlan:add']" icon="Plus" plain type="primary"
              @click="handleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['painting:workPlan:edit']" :disabled="single" icon="Edit" plain type="success"
              @click="handleUpdate()">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['painting:workPlan:remove']" :disabled="multiple" icon="Delete" plain type="danger"
              @click="handleDelete()">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['painting:workPlan:export']" icon="Download" plain type="warning"
              @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="workPlanList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55" />
        <!-- <el-table-column v-if="true" align="center" label="主键ID" prop="id" /> -->
        <!-- <el-table-column align="center" label="状态：0-禁用，1-启用" prop="status" /> -->
        <el-table-column align="center" label="状态" prop="status">
          <template #default="scope">
            <span v-if="scope.row.status == 0">禁用</span>
            <span v-if="scope.row.status == 1">启用</span>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="描述" prop="description" /> -->
        <!-- <el-table-column align="center" label="是否删除：0-未删除，1-已删除" prop="isDeleted" /> -->
        <!-- <el-table-column align="center" label="是否删除" prop="isDeleted">
          <template #default="scope">
            <span v-if="scope.row.status == 0">未删除</span>
            <span v-if="scope.row.status == 1">已删除</span>
          </template>
        </el-table-column> -->
        <el-table-column align="center" label="订单号" prop="orderNumber" />
        <el-table-column align="center" label="车辆号" prop="vehicleNumber" />
        <!-- <el-table-column align="center" label="工艺路线编码" prop="processRouteCode" /> -->
        <el-table-column align="center" label="工序号" prop="processNumber" />
        <el-table-column align="center" label="工序名称" prop="processName" />
        <!-- <el-table-column align="center" label="工作中心编码" prop="workCenterCode" /> -->
        <el-table-column align="center" label="工作中心名称" prop="workCenterName" />
        <el-table-column align="center" label="工作中心描述" prop="workCenterDescription" />
        <el-table-column align="center" label="计划开始时间" prop="planStartTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.planStartTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="计划完工时间" prop="planEndTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.planEndTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="实际开工时间" prop="actualStartTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.actualStartTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="实际完工时间" prop="actualEndTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.actualEndTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="作业人ID" prop="operator" /> -->
        <el-table-column align="center" label="下发状态" prop="dispatchStatus">
          <template #default="scope">
            <span v-if="scope.row.status == 0">未下发</span>
            <span v-if="scope.row.status == 1">已下发</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="作业状态" prop="workStatus">
          <template #default="scope">
            <span v-if="scope.row.status == 0">未开工</span>
            <span v-if="scope.row.status == 1">已开工</span>
            <span v-if="scope.row.status == 2">完工待检</span>
            <span v-if="scope.row.status == 3">完工</span>
          </template>
        </el-table-column>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="250px">
          <template #default="scope">
            <!-- <el-button link type="primary" @click="">查看</el-button> -->
            <!-- <el-button link type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button link type="primary" @click="handleIssued(scope.row)">下发</el-button>
            <el-button link type="primary" @click="">开工</el-button>
            <el-button link type="primary" @click="">完工</el-button>
            <el-button link type="primary" @click="handleAbnormal(scope.row)">异常上报</el-button>
            <el-button link type="primary" @click="">自检</el-button>
            <el-button link type="primary" @click="handleInspection(scope.row)">互检</el-button> -->
            <!-- <el-tooltip content="修改" placement="top"> -->
            <!-- <el-button v-hasPermi="['painting:workPlan:edit']" icon="Edit" link type="primary" @click="handleUpdate(scope.row)">修改</el-button> -->
            <!-- </el-tooltip> -->
            <!-- <el-tooltip content="查看" placement="top"> -->
            <!-- </el-tooltip> -->
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['painting:workPlan:edit']" icon="Edit" link type="primary" @click="handleUpdate(scope.row)"></el-button>
           </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['painting:workPlan:remove']" icon="Delete" link type="primary" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
        :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改工作计划对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" append-to-body width="800px">
      <el-form ref="workPlanFormRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入内容" type="textarea" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" class="m-2" placeholder="请选择状态" size="large">
            <el-option v-for="item in optionsStatus" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否删除" prop="isDeleted">
          <!-- <el-input v-model="form.isDeleted" placeholder="请输入是否删除：0-未删除，1-已删除" /> -->
          <el-select v-model="form.isDeleted" class="m-2" placeholder="请选择">
            <el-option v-for="item in isDeletedOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="下发状态" prop="dispatchStatus">
          <!-- <el-input v-model="form.isDeleted" placeholder="请输入是否删除：0-未删除，1-已删除" /> -->
          <el-select v-model="form.dispatchStatus" class="m-2" placeholder="请选择">
            <el-option v-for="item in dispatchStatusOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="作业状态" prop="workStatus">
          <!-- <el-input v-model="form.isDeleted" placeholder="请输入是否删除：0-未删除，1-已删除" /> -->
          <el-select v-model="form.workStatus" class="m-2" placeholder="请选择">
            <el-option v-for="item in workStatusOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="订单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="车辆号" prop="vehicleNumber">
          <el-input v-model="form.vehicleNumber" placeholder="请输入车辆号" />
        </el-form-item>
        <el-form-item label="工艺路线编码" prop="processRouteCode">
          <el-input v-model="form.processRouteCode" placeholder="请输入工艺路线编码" />
        </el-form-item>
        <el-form-item label="工序号" prop="processNumber">
          <el-input v-model="form.processNumber" placeholder="请输入工序号" />
        </el-form-item>
        <el-form-item label="工序名称" prop="processName">
          <el-input v-model="form.processName" placeholder="请输入工序名称" />
        </el-form-item>
        <el-form-item label="工作中心编码" prop="workCenterCode">
          <el-input v-model="form.workCenterCode" placeholder="请输入工作中心编码" />
        </el-form-item>
        <el-form-item label="工作中心名称" prop="workCenterName">
          <el-input v-model="form.workCenterName" placeholder="请输入工作中心名称" />
        </el-form-item>
        <el-form-item label="工作中心描述" prop="workCenterDescription">
          <el-input v-model="form.workCenterDescription" placeholder="请输入内容" type="textarea" />
        </el-form-item>
        <el-form-item label="计划开始时间" prop="planStartTime">
          <el-date-picker v-model="form.planStartTime" clearable placeholder="请选择计划开始时间" type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划完工时间" prop="planEndTime">
          <el-date-picker v-model="form.planEndTime" clearable placeholder="请选择计划完工时间" type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际开工时间" prop="actualStartTime">
          <el-date-picker v-model="form.actualStartTime" clearable placeholder="请选择实际开工时间" type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际完工时间" prop="actualEndTime">
          <el-date-picker v-model="form.actualEndTime" clearable placeholder="请选择实际完工时间" type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="作业人ID" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入作业人ID" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 下发 -->
    <el-dialog v-model="dialogIssued.visible" :title="dialogIssued.title" append-to-body width="800px">
      <el-form ref="workPlanFormRef" :model="formAbnormal" :rules="rules" label-width="120px">
        <el-form-item label="工位" prop="isDeleted">

          <el-select v-model="form.operatorId" :loading="workstationLoading" :remote-method="handleIssuedList" clearable
            filterable placeholder="请选择" remote>
            <el-option v-for="item in issuedList" :key="item.id" :label="item.workstationName"
              :value="item.id">
              <span>{{ item.workstationName }}</span>
              <!-- <span class="text-gray-500 ml-2">({{ item.workstationCode }})</span> -->
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancelAbnormal">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 异常上报 -->
    <el-dialog v-model="dialogAbnormal.visible" :title="dialogAbnormal.title" append-to-body width="800px">
      <el-form ref="workPlanFormRef" :model="formAbnormal" :rules="rules" label-width="120px">
        <el-form-item label="异常类别" prop="status">
          <el-select v-model="formAbnormal.status" class="m-2" placeholder="请选择" size="large">
            <el-option v-for="item in optionsStatus" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="异常描述" prop="description">
          <el-input v-model="formAbnormal.description" placeholder="请输入" type="textarea" />
        </el-form-item>
        <el-form-item label="异常处理人" prop="isDeleted">
          <!-- <el-input v-model="form.isDeleted" placeholder="请输入是否删除：0-未删除，1-已删除" /> -->
          <!-- <el-select v-model="formAbnormal.isDeleted" class="m-2" placeholder="请选择">
            <el-option v-for="item in isDeletedOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select> -->
          <el-select v-model="form.operatorId" :loading="operatorIdLoading" :remote-method="handleOperatorIdSearch"
            clearable filterable placeholder="请选择" remote>
            <el-option v-for="item in operatorOptions" :key="item.userId" :label="item.nickName" :value="item.userId">
              <span>{{ item.nickName }}</span>
              <!-- <span class="text-gray-500 ml-2">({{ item.workstationCode }})</span> -->
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="异常上报人" prop="operator">
          <el-input v-model="formAbnormal.operator" placeholder="请输入作业人ID" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancelAbnormal">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 互检 -->
    <el-dialog v-model="dialogInspection.visible" :title="dialogInspection.title" append-to-body width="500px">
      <el-form ref="workPlanFormRef" :model="formInspection" :rules="rules" label-width="120px">

        <el-form-item label="账号" prop="user">
          <el-input v-model="formInspection.user" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="formInspection.pwd" placeholder="请输入密码" type="password" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitFormInspection">确 定</el-button>
          <el-button @click="cancelInspection">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" name="WorkPlan" setup>
import { addWorkPlan, delWorkPlan, getWorkPlan, listWorkPlan, updateWorkPlan } from '@/api/painting/workPlan';
import { WorkPlanForm, WorkPlanQuery, WorkPlanVO } from '@/api/painting/workPlan/types';
import { useUserStore } from '@/store/modules/user';
import api from '@/api/system/user';
import { listWorkstation } from '@/api/painting/workstation';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const workPlanList = ref<WorkPlanVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const workPlanFormRef = ref<ElFormInstance>();


const userStore = useUserStore();
// 可以获取以下用户信息
const userId = userStore.userId;
const tenantId = userStore.tenantId;
const nickname = userStore.nickname;
const avatar = userStore.avatar;
const roles = userStore.roles;
const permissions = userStore.permissions;
// 当前操作人ID
const operatorIdLoading = ref(false);
const operatorOptions = ref<UserVO[]>([]);
// 选择工位
const issuedList = ref([]);
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const isDeletedOptions = ref([{
  label: '未删除',
  value: '0'
}, {
  label: '已删除',
  value: '1'
}])
const optionsStatus = ref([
  {
    value: 0,
    label: '禁用',
  },
  {
    value: 1,
    label: '启用',
  },
])
const dispatchStatusOptions = ref([
  {
    value: 0,
    label: '未下发',
  },
  {
    value: 1,
    label: '已下发',
  },
])

const workStatusOptions = ref([
  {
    value: 0,
    label: '未开工',
  },
  {
    value: 1,
    label: '已开工',
  },
  {
    value: 2,
    label: '完工待检',
  },
  {
    value: 3,
    label: '完工',
  },
])
const initFormData: WorkPlanForm = {
  id: undefined,
  status: undefined,
  description: undefined,
  isDeleted: undefined,
  orderNumber: undefined,
  vehicleNumber: undefined,
  processRouteCode: undefined,
  processNumber: undefined,
  processName: undefined,
  workCenterCode: undefined,
  workCenterName: undefined,
  workCenterDescription: undefined,
  planStartTime: undefined,
  planEndTime: undefined,
  actualStartTime: undefined,
  actualEndTime: undefined,
  operator: undefined,
  dispatchStatus: undefined,
  workStatus: undefined
}
const formAbnormal = ref({
  status: null,
  description: null,
  isDeleted: null,
  operator: null
})
const formInspection = ref({
  user: null,
  pwd: null,
})
const data = reactive<PageData<WorkPlanForm, WorkPlanQuery>>({
  form: { ...initFormData },

  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: undefined,
    description: undefined,
    isDeleted: undefined,
    orderNumber: undefined,
    vehicleNumber: undefined,
    processRouteCode: undefined,
    processNumber: undefined,
    processName: undefined,
    workCenterCode: undefined,
    workCenterName: undefined,
    workCenterDescription: undefined,
    planStartTime: undefined,
    planEndTime: undefined,
    actualStartTime: undefined,
    actualEndTime: undefined,
    operator: undefined,
    dispatchStatus: undefined,
    workStatus: undefined,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "主键ID不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
    isDeleted: [
      { required: true, message: "是否删除不能为空", trigger: "blur" }
    ],
    orderNumber: [
      { required: true, message: "订单号不能为空", trigger: "blur" }
    ],
    vehicleNumber: [
      { required: true, message: "车辆号不能为空", trigger: "blur" }
    ],
    processRouteCode: [
      { required: true, message: "工艺路线编码不能为空", trigger: "blur" }
    ],
    processNumber: [
      { required: true, message: "工序号不能为空", trigger: "blur" }
    ],
    processName: [
      { required: true, message: "工序名称不能为空", trigger: "blur" }
    ],
    workCenterCode: [
      { required: true, message: "工作中心编码不能为空", trigger: "blur" }
    ],

    planStartTime: [
      { required: true, message: "计划开始时间不能为空", trigger: "blur" }
    ],
    planEndTime: [
      { required: true, message: "计划完工时间不能为空", trigger: "blur" }
    ],
    dispatchStatus: [
      { required: true, message: "下发状态 0-未下发 1-已下发不能为空", trigger: "change" }
    ],
    workStatus: [
      { required: true, message: "作业状态 0未开工 1已开工 2完工待检 3完工不能为空", trigger: "change" }
    ],
    user: [
      { required: true, message: "账号不能为空", trigger: "blur" }
    ],
    pwd: [
      { required: true, message: "密码不能为空", trigger: "blur" }
    ],
  }
});
// 工位列表数据

const workstationLoading = ref(false);
const handleIssuedList = async (query: string) => {
  workstationLoading.value = true;
  try {
    const res = await listWorkstation({
      pageSize: 10,
      pageNum: 1
    });
    issuedList.value = res.rows;
  } finally {
    workstationLoading.value = false;
  }
}
// 异常处理人
const handleOperatorIdSearch = async (query: string) => {
  operatorIdLoading.value = true;
  try {
    // const res = await listUser({
    //   workstationName: query,
    //   pageSize: 10,
    //   pageNum: 1
    // });
    const res = await api.listUser(proxy?.addDateRange(queryParams.value, ''));

    operatorOptions.value = res.rows;
  } finally {
    operatorIdLoading.value = false;
  }
}
// 异常上报
const dialogAbnormal = reactive<DialogOption>({
  visible: false,
  title: ''
});
// 互检人弹窗
const dialogInspection = reactive<DialogOption>({
  visible: false,
  title: ''
});
// 下发
const dialogIssued= reactive<DialogOption>({
  visible: false,
  title: ''
});
const { queryParams, form, rules } = toRefs(data);

/** 查询工作计划列表 */
const getList = async () => {
  loading.value = true;
  const res = await listWorkPlan(queryParams.value);
  workPlanList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}
// 下发
const handleIssued = (row) => {
  console.log(row)
  dialogIssued.visible = true;
  dialogIssued.title = "下发";
}
// 异常上报
const handleAbnormal = (row) => {
  console.log(row)
  dialogAbnormal.visible = true;
  dialogAbnormal.title = "异常上报";
}
const cancelAbnormal = () => {
  reset();
  dialogAbnormal.visible = false;
}
// 互检人
const handleInspection = (row) => {
  console.log(row)
  dialogInspection.visible = true;
  dialogInspection.title = "互检人信息";
}
const cancelInspection = () => {
  reset();
  dialogInspection.visible = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  workPlanFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: WorkPlanVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加工作计划";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: WorkPlanVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getWorkPlan(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改工作计划";
}


/** 提交按钮 */
const submitForm = () => {
  workPlanFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateWorkPlan(form.value).finally(() => buttonLoading.value = false);
      } else {
        await addWorkPlan(form.value).finally(() => buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}
// 互检信息
const submitFormInspection = () => {
  workPlanFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      // if (form.value.id) {
      //   await updateWorkPlan(form.value).finally(() => buttonLoading.value = false);
      // } else {
      //   await addWorkPlan(form.value).finally(() => buttonLoading.value = false);
      // }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: WorkPlanVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除工作计划编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delWorkPlan(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('painting/workPlan/export', {
    ...queryParams.value
  }, `workPlan_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
